﻿

using Csv;
using MysqlforDataWatch;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Text;


namespace Tools.CSVHelper
{
    public class CSVHelperClass
    {
        public static void saveCSVFile(List<StatisticAll> list)
        {
            //序列化对象为json字符串
            string csv = CsvSerializer.Serialize(list, withHeaders: true);
            string path = Assembly.GetExecutingAssembly().Location;
            string directory = Path.GetDirectoryName(path);
            string writepath = Path.Combine(directory, "mileagecsv.csv");
            File.WriteAllText(writepath, csv);
        }

        public static void saveCSVFileTranspose(List<StatisticAll> list)
        {
            string path = Assembly.GetExecutingAssembly().Location;
            string directory = Path.GetDirectoryName(path);
            string writepath = Path.Combine(directory, "mileagetransposecsv.csv");
            var chantitle = list.Select(s => s.chantitle).Distinct().ToList();
            var channumber = chantitle.Count();
            List<string> contents = new List<string>();
            string header = null;
           
            for (int i = 0; i < channumber; i++)
            {
                if (i == 0)
                {
                    header = $"vehicle,filename,mileage,{chantitle[i]}_max,{chantitle[i]}_min,{chantitle[i]}_range,{chantitle[i]}_mean,{chantitle[i]}_variance,{chantitle[i]}_std,{chantitle[i]}_rms,{chantitle[i]}_k3,{chantitle[i]}_k5";
                }
                else
                {
                    string header2 =$"{chantitle[i]}_max,{chantitle[i]}_min,{chantitle[i]}_range,{chantitle[i]}_mean,{chantitle[i]}_variance,{chantitle[i]}_std,{chantitle[i]}_rms,{chantitle[i]}_k3,{chantitle[i]}_k5";
                    header = string.Join(',', header, header2);
                }
            }
            contents.Add(header);
            string line = null;
            using (var writer = new StreamWriter(writepath))
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (i == 0||i%channumber==0)//i能整除通道数就说明是新的一公里数据
                    {
                        //一开始line肯定为null，不能添加进去
                        if (!string.IsNullOrEmpty(line))
                        {
                            contents.Add(line);//每次到下一个文件时需要先把之前的line给加进去再去清空
                            line = null;
                        }
                       
                        line = $"{list[i].vehicle},{list[i].filename},{list[i].mileage},{list[i].max},{list[i].min},{list[i].range},{list[i].mean},{list[i].variance},{list[i].std},{list[i].rms},{list[i].damagek3},{list[i].damagek5}";
                        
                    }
                    else
                    {
                        line= String.Join(", ", line,list[i].max,list[i].min,list[i].range,list[i].mean,list[i].variance,list[i].std,list[i].rms,list[i].damagek3,list[i].damagek5);
                    }
                 
                }
                if (!string.IsNullOrEmpty(line))
                {
                    contents.Add(line);//最后一段因为不会进入if判断是否整除，所以循环结束后肯定还留有最后一段的数据，添加进去
                  
                }
            }

            File.WriteAllLinesAsync(writepath, contents);
        }
    }
}
